package com.zxmr.hospital.customer.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Customer;
import com.zxmr.domain.SurgeryAppointment;
import com.zxmr.domain.account.User;
import com.zxmr.domain.service.CustomerService;
import com.zxmr.domain.service.SurgeryAppointmentService;
import com.zxmr.hospital.customer.dto.JSONListDTO;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;
import com.zxmr.util.Utils;

public class SurgeryAppointAction extends Executor{

	enum method {
		add,
		query,
		edit;
	}
	@Override
	public void execute(Request request, Response response) {
		
		try
		{
			method method = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(method)
			{
			case add:
				add(request,response);
				break;
			case query:
				query(request,response);
				break;
			case edit:
				edit(request,response);
			}

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}

	public void add(Request request,Response response)throws ExecuteException
	{
		SurgeryAppointment ca = parse(request);
		validateAddCA(ca,request.getAccount());
		ca.setCompany(request.getAccount().getCompany());
		ca.setCreateTime(Utils.getCurrentTime());
		ca.setCreateUser(request.getAccount().getName());
		
		SurgeryAppointmentService service = new SurgeryAppointmentService();
		if(!service.add(ca))this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.customer.addSurgeryAppoint",
					ca.getCustomerName(),ca.getItems(),ca.getTime());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
	private void validateAddCA(SurgeryAppointment ca,User user)throws ExecuteException
	{
		if(ca == null)throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
		if(Utils.isStringEmptyWithTrim(ca.getTime()))throw new ExecuteException(
				ResourceManager.getInstance().getMessage("com.zxmr.customer.ca.notime"));
		
		CustomerService service = new CustomerService();
		Customer customer = service.queryCustomer(ca.getCustomer());
		if(customer == null || customer.getCompany()!=user.getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.misCustomer"));
	}
	private SurgeryAppointment parse(Request request)throws ExecuteException
	{
		try
		{
			return JSON.parseObject(request.getRequestParameters().get("parameters"), SurgeryAppointment.class);
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.errorPara"));
		}
	}
	public void query(Request request,Response response)throws ExecuteException
	{	
		SurgeryAppointmentService service = new SurgeryAppointmentService();
		int count = getCountPerPage(request);
		int start = getCountStart(request);
		
		List<SurgeryAppointment> cas = service.queryCAPagination(start, count, request.getAccount().getCompany());
		int total = service.getTotalCount(request.getAccount().getCompany());
		
		JSONListDTO list = new JSONListDTO();
		list.setRows(cas);
		list.setTotal(total);
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(list));
		response.setPayload(payload);
	}
	
	private int getCountPerPage(Request request)
	{
		String count = request.getRequestParameters().get("size");
		if(count == null || "".equals(count.trim()))return 10;
		try
		{
			return Integer.parseInt(count);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 10;
		}
	}
	private int getCountStart(Request request)
	{
		String start = request.getRequestParameters().get("start");
		if(start == null || "".equals(start.trim()))return 0;
		try
		{
			return Integer.parseInt(start);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private void edit(Request request,Response response)throws ExecuteException
	{
		SurgeryAppointment ca = parse(request);
		SurgeryAppointmentService service = new SurgeryAppointmentService();
		SurgeryAppointment old = service.query(ca.getId());
		
		if(request.getAccount().getCompany() != old.getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.noauth"));
		
		// set field that can not be updated
		ca.setCompany(old.getCompany());
		ca.setCreateTime(old.getCreateTime());
		ca.setCreateUser(old.getCreateUser());
		
		if(!service.update(ca))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.customer.editSurgeryAppoint",
					ca.getCustomerName(),ca.getItems(),ca.getTime());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
}
